import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { MessageService } from './../core/message.service';
import { ApiService } from './../core/api/api.service';
import { ToptipsService } from 'ngx-weui';
import { I18nService } from '../core/i18n/i18n.service';

@Component({
	templateUrl: './updatePwd.html',
	styleUrls: ['./updatePwd.less'],
})
export class UpdatePwdComponent implements OnInit {
	data: any = {};
	active = 1;
	type;
	disabled = false;
	userDel;
	title;
	iData;
	constructor(private i: I18nService, private route: Router, private activatedRoute: ActivatedRoute, private apiService: ApiService, private srv: ToptipsService) {
		this.i.get([
			'MSG.register-code', 
			'MSG.dlmmok', 
		'registerPage.input-mailCode-error', 
		'updatePwd-page.xgdlmm', 
		'updatePwd-page.xgzfmm', 
		'updatePwd-page.wjmm', 
		'error.yxyzm', 
		'error.dlmm', 		
		'error.zfmm', 		
		'error.redlmm', 		
		'error.rezfmm', 
		'MSG.zfmmok'		
	]).subscribe(res => {
			this.iData = res;
			this.activatedRoute.params.subscribe((params: Params) => {
				this.type = Number(params['type']);
				this.title = this.type === 1 ? this.iData['updatePwd-page.xgdlmm']: this.type === 2? this.iData['updatePwd-page.xgzfmm'] : this.iData['updatePwd-page.wjmm'] ;
				if (this.type !== 3) {
					this.apiService.post('用户详情', {}).then(res => {
						this.userDel = res['data'];
						this.data.email = this.userDel.email;
					});
				}
			});
		});
	}
	ngOnInit() {
	}
	showThis(i) {
		if (this.active !== i) {
			this.active = i;
		}
	}
	back(){
		if(this.type == 3){
			this.route.navigateByUrl('/login');
		}else{
			this.route.navigateByUrl('/security');
		}
	}
	sendCode(e) {
		const element = e.target;
		if(this.data.email){

			if (!this.disabled) {
				this.disabled = true;
				this.apiService
					.post('发送邮箱验证码', { email: this.data.email, type: 'repwd' })
					.then(res => {
						this.srv.success(this.iData['MSG.register-code']);
						let t = 60;
						element.innerHTML = t + 's' + this.iData['registerPage.input-mailCode-error'];
						const i = setInterval(() => {
							t--;
							element.innerHTML = t + 's' + this.iData['registerPage.input-mailCode-error'];
							if (t < 1) {
								clearInterval(i);
								this.disabled = false;
							}
						}, 1000);
					})
			}
		}
	}
	sub() {
		if (!this.data.verifyCode) {
			this.srv.warn(this.iData['error.yxyzm']);
			return;
		}
		if (this.type === 1 || this.type === 3) {
			if (!this.data.loginPw) {
				this.srv.warn(this.iData['error.dlmm']);
				return;
			}
			if (!this.data.reLoginPw) {
				this.srv.warn(this.iData['error.redlmm']);
				return;
			}
			let url = this.type == 1 ? '修改登录密码' : '忘记密码';
 			this.apiService.post(url, this.data).then(res => {
				this.srv.success(this.iData['MSG.dlmmok']);
				this.type == 1 ? this.route.navigateByUrl('/security') : this.route.navigateByUrl('/login');
			});
		} else if (this.type === 2) {
			if (!this.data.tradePw) {
				this.srv.warn(this.iData['error.zfmm']);
				return;
			}
			if (!this.data.reTradePw) {
				this.srv.warn(this.iData['error.rezfmm']);
				return;
			}
			this.apiService.post('修改支付密码', this.data).then(res => {
				this.srv.success(this.iData['MSG.zfmmok']);
				this.route.navigateByUrl('/security');
			});
		}
		
	}
}
